package com.zmsoft.boxy.sfg.service;

import org.apache.commons.lang3.RandomUtils;
import org.cnsoft.framework.beans.common.RESTResultBean;
import org.cnsoft.framework.utils.EmptyHelper;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;

import com.zmsoft.boxy.sfg.player.BoxySFGPlayer;
import com.zmsoft.boxy.sfg.player.SfgPlayerFriendDBO;
import com.zmsoft.persistent.sfg.Sfg661010PlayerFarm.Sfg661010PlayerFarmDBO;
import com.zmsoft.persistent.sfg.Sfg661021PlayerLand.Sfg661021PlayerLandDBO;
import com.zmsoft.persistent.sfg.Sfg661030PlayerWarehouse.Sfg661030PlayerWarehouseDBO;
import com.zmsoft.persistent.sfg.Sfg662040GameCrops.Sfg662040GameCropsDBO;
import com.zmsoft.persistent.sfg.Sfg662060GameInteract.Sfg662060GameInteractDBO;

/**
 * 游戏玩家间交互
 * 
 * @author ZmSoft
 * @version 0.1.0 2018/3/1
 * @since 0.1.0 2018/3/1
 */
@Service("SFGGameInteractService")
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)
//@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class SFGGameInteractService extends MySFGGameServiceSupport {

	/**
	 * 浇水
	 */
	public RESTResultBean<BoxySFGPlayer> doInteractWatering(BoxySFGPlayer player) throws Exception {
		RESTResultBean<BoxySFGPlayer> result = new RESTResultBean<BoxySFGPlayer>();
		// 用户农场信息
		Sfg661010PlayerFarmDBO playerFarm = sfgPlayerInfoCacheService.loadPlayerFarm(player);
		//好友农场信息
//		Sfg661010PlayerFarmDBO playerFriendFarm = sfgPlayerInfoCacheService.loadPlayerFarm(player.getPlayerFriendId());
		// 获得好友用户土地信息
		Sfg661021PlayerLandDBO playerFriendLand = sfgPlayerInfoCacheService.loadPlayerLand(player.getPlayerFriendId(), player);// 获得物品信息
		// 获得物品信息
		Sfg662040GameCropsDBO gameCrop = sfgGameInfoCacheService.loadGameCropsData(SFG_GAME_FARM_TYPE, playerFriendLand.getCropsId());

		// 交互动作配置
		Sfg662060GameInteractDBO gameInteract = sfgGameInfoCacheService.loadGameInteractData(GAME_INTERACT, playerFriendLand.getActionId());
		if (EmptyHelper.isNotEmpty(gameInteract) && Interact_Command_doLandPlantWatering.equals(gameInteract.getActionCommandApi())) {
			// 动作增加经验, 农场升级信息
			SFGGameThrusterServiceHelper.preparePlayerFarmTodayExp(sfgGameInfoCacheService, sfgPlayerInfoCacheService, playerFarm, gameInteract.getAwardExp());
		}
		// 好友减少次数
		{
			// 剩余浇水数目
			long leftWatering = playerFriendLand.getLeftWatering() - 1;
			if (leftWatering < 1) {
				leftWatering = 0;
				// 阶段互动ID
				playerFriendLand.setActionId(BLANK);
				// 阶段互动名称
				playerFriendLand.setActionName(BLANK);
				// 功能操作指令
				playerFriendLand.setActionCommandPage(BLANK);
				// 服务操作指令
				playerFriendLand.setActionCommandApi(BLANK);
			}
			// 设置剩余浇水数目
			playerFriendLand.setLeftWatering(leftWatering);
			// 作物增产
			SFGGameThrusterServiceHelper.peducePlayerLandCrops(sfgGameInfoCacheService, sfgPlayerInfoCacheService, playerFriendLand, gameCrop, 65);
		}

		// 设置返回信息
		result.setMsg(ACTION_OK_MESSAGE);
		// 用户数据集中保存
		sfgPlayerInfoCacheService.savePlayerFarm(playerFarm);
//		sfgPlayerInfoCacheService.savePlayerFarm(playerFriendFarm);
		sfgPlayerInfoCacheService.savePlayerLand(player, playerFriendLand);
		return result;

	}

	/**
	 * 施肥
	 */
	public RESTResultBean<BoxySFGPlayer> doLandPlantFertilize(BoxySFGPlayer player) throws Exception {
		RESTResultBean<BoxySFGPlayer> result = new RESTResultBean<BoxySFGPlayer>();
		// 用户农场信息
		Sfg661010PlayerFarmDBO playerFarm = sfgPlayerInfoCacheService.loadPlayerFarm(player);
		//好友农场信息
//		Sfg661010PlayerFarmDBO playerFriendFarm = sfgPlayerInfoCacheService.loadPlayerFarm(player.getPlayerFriendId());
		// 获得好友用户土地信息
		Sfg661021PlayerLandDBO playerFriendLand = sfgPlayerInfoCacheService.loadPlayerLand(player.getPlayerFriendId(), player);// 获得物品信息
		// 获得物品信息
		Sfg662040GameCropsDBO gameCrop = sfgGameInfoCacheService.loadGameCropsData(SFG_GAME_FARM_TYPE, playerFriendLand.getCropsId());

		// 交互动作配置
		Sfg662060GameInteractDBO gameInteract = sfgGameInfoCacheService.loadGameInteractData(GAME_INTERACT, playerFriendLand.getActionId());
		if (EmptyHelper.isNotEmpty(gameInteract) && Interact_Command_doLandPlantFertilize.equals(gameInteract.getActionCommandApi())) {
			// 动作增加经验, 农场升级信息
			SFGGameThrusterServiceHelper.preparePlayerFarmTodayExp(sfgGameInfoCacheService, sfgPlayerInfoCacheService, playerFarm, gameInteract.getAwardExp());
		}
		// 好友减少次数
		{
			// 剩余施肥数目
			long leftFertilize = playerFriendLand.getLeftFertilize() - 1;
			if (leftFertilize < 1) {
				leftFertilize = 0;
				// 阶段互动ID
				playerFriendLand.setActionId(BLANK);
				// 阶段互动名称
				playerFriendLand.setActionName(BLANK);
				// 功能操作指令
				playerFriendLand.setActionCommandPage(BLANK);
				// 服务操作指令
				playerFriendLand.setActionCommandApi(BLANK);
			}
			// 设置剩余施肥数目
			playerFriendLand.setLeftFertilize(leftFertilize);
			// 作物增产
			SFGGameThrusterServiceHelper.peducePlayerLandCrops(sfgGameInfoCacheService, sfgPlayerInfoCacheService, playerFriendLand, gameCrop, 65);
		}

		// 设置返回信息
		result.setMsg(ACTION_OK_MESSAGE);
		// 用户数据集中保存
		sfgPlayerInfoCacheService.savePlayerFarm(playerFarm);
//		sfgPlayerInfoCacheService.savePlayerFarm(playerFriendFarm);
		sfgPlayerInfoCacheService.savePlayerLand(player, playerFriendLand);
		return result;
	}

	/**
	 * 除草
	 */
	public RESTResultBean<BoxySFGPlayer> doInteractWeed(BoxySFGPlayer player) throws Exception {
		RESTResultBean<BoxySFGPlayer> result = new RESTResultBean<BoxySFGPlayer>();
		// 用户农场信息
		Sfg661010PlayerFarmDBO playerFarm = sfgPlayerInfoCacheService.loadPlayerFarm(player);
		//好友农场信息
//		Sfg661010PlayerFarmDBO playerFriendFarm = sfgPlayerInfoCacheService.loadPlayerFarm(player.getPlayerFriendId());
		// 获得好友用户土地信息
		Sfg661021PlayerLandDBO playerFriendLand = sfgPlayerInfoCacheService.loadPlayerLand(player.getPlayerFriendId(), player);// 获得物品信息
		// 获得物品信息
		Sfg662040GameCropsDBO gameCrop = sfgGameInfoCacheService.loadGameCropsData(SFG_GAME_FARM_TYPE, playerFriendLand.getCropsId());

		// 交互动作配置
		Sfg662060GameInteractDBO gameInteract = sfgGameInfoCacheService.loadGameInteractData(GAME_INTERACT, playerFriendLand.getActionId());
		if (EmptyHelper.isNotEmpty(gameInteract) && Interact_Command_doLandPlantWeed.equals(gameInteract.getActionCommandApi())) {
			// 动作增加经验, 农场升级信息
			SFGGameThrusterServiceHelper.preparePlayerFarmTodayExp(sfgGameInfoCacheService, sfgPlayerInfoCacheService, playerFarm, gameInteract.getAwardExp());
		}
		// 好友减少次数
		{
			// 剩余除草数目
			long leftWeeding = playerFriendLand.getLeftWeeding() - 1;
			if (leftWeeding < 1) {
				leftWeeding = 0;
			// 阶段互动ID
				// 阶段互动ID
				playerFriendLand.setActionId(BLANK);
				// 阶段互动名称
				playerFriendLand.setActionName(BLANK);
				// 功能操作指令
				playerFriendLand.setActionCommandPage(BLANK);
				// 服务操作指令
				playerFriendLand.setActionCommandApi(BLANK);
			}
			// 设置剩余除草数目
			playerFriendLand.setLeftWeeding(leftWeeding);
			// 作物增产
			SFGGameThrusterServiceHelper.peducePlayerLandCrops(sfgGameInfoCacheService, sfgPlayerInfoCacheService, playerFriendLand, gameCrop, 65);
		}

		// 设置返回信息
		result.setMsg(ACTION_OK_MESSAGE);
		// 用户数据集中保存
		sfgPlayerInfoCacheService.savePlayerFarm(playerFarm);
//		sfgPlayerInfoCacheService.savePlayerFarm(playerFriendFarm);
		sfgPlayerInfoCacheService.savePlayerLand(player, playerFriendLand);
		return result;
	}

	/**
	 * 杀虫
	 */
	public RESTResultBean<BoxySFGPlayer> doInteractInsecticidal(BoxySFGPlayer player) throws Exception {

		RESTResultBean<BoxySFGPlayer> result = new RESTResultBean<BoxySFGPlayer>();
		// 用户农场信息
		Sfg661010PlayerFarmDBO playerFarm = sfgPlayerInfoCacheService.loadPlayerFarm(player);
		//好友农场信息
//		Sfg661010PlayerFarmDBO playerFriendFarm = sfgPlayerInfoCacheService.loadPlayerFarm(player.getPlayerFriendId());
		// 获得好友用户土地信息
		Sfg661021PlayerLandDBO playerFriendLand = sfgPlayerInfoCacheService.loadPlayerLand(player.getPlayerFriendId(), player);// 获得物品信息
		// 获得物品信息
		Sfg662040GameCropsDBO gameCrop = sfgGameInfoCacheService.loadGameCropsData(SFG_GAME_FARM_TYPE, playerFriendLand.getCropsId());

		// 交互动作配置
		Sfg662060GameInteractDBO gameInteract = sfgGameInfoCacheService.loadGameInteractData(GAME_INTERACT, playerFriendLand.getActionId());
		if (EmptyHelper.isNotEmpty(gameInteract) && Interact_Command_doLandPlantInsecticidal.equals(gameInteract.getActionCommandApi())) {
			// 动作增加经验, 农场升级信息
			SFGGameThrusterServiceHelper.preparePlayerFarmTodayExp(sfgGameInfoCacheService, sfgPlayerInfoCacheService, playerFarm, gameInteract.getAwardExp());
		}
		// 好友减少次数
		{
			// 剩余杀虫数目
			long leftInsect = playerFriendLand.getLeftInsect() - 1;
			if (leftInsect < 1) {
				leftInsect = 0;
			// 阶段互动ID
				// 阶段互动ID
				playerFriendLand.setActionId(BLANK);
				// 阶段互动名称
				playerFriendLand.setActionName(BLANK);
				// 功能操作指令
				playerFriendLand.setActionCommandPage(BLANK);
				// 服务操作指令
				playerFriendLand.setActionCommandApi(BLANK);
			}
			// 设置剩余浇水数目
			playerFriendLand.setLeftInsect(leftInsect);
			// 作物增产
			SFGGameThrusterServiceHelper.peducePlayerLandCrops(sfgGameInfoCacheService, sfgPlayerInfoCacheService, playerFriendLand, gameCrop, 65);
		}

		// 设置返回信息
		result.setMsg(ACTION_OK_MESSAGE);
		// 用户数据集中保存
		sfgPlayerInfoCacheService.savePlayerFarm(playerFarm);
//		sfgPlayerInfoCacheService.savePlayerFarm(playerFriendFarm);
		sfgPlayerInfoCacheService.savePlayerLand(player, playerFriendLand);
		return result;
	}

	/**
	 * 摘取果实
	 */
	public RESTResultBean<BoxySFGPlayer> doInteractSteal(BoxySFGPlayer player) throws Exception {
		RESTResultBean<BoxySFGPlayer> result = new RESTResultBean<BoxySFGPlayer>();
		// 用户农场信息
		Sfg661010PlayerFarmDBO playerFarm = sfgPlayerInfoCacheService.loadPlayerFarm(player);
		// 获得用户土地信息
		Sfg661021PlayerLandDBO playerLand = sfgPlayerInfoCacheService.loadPlayerLand(player);
		// 判断作物状态
		if (SIX.equals(playerLand.getStageId()) == false) {
			// 非法操作
			result.setCode(1);
			result.setMsg(FAIL_MESSAGE_BUG);
			return result;
		}

		// 补全作物ID
		player.setPlayerPropId(playerLand.getCropsId());
		// 用户仓库数据
		Sfg661030PlayerWarehouseDBO playerWarehouseProp = sfgPlayerInfoCacheService.loadPlayerWarehouseProp(player);
		// 果实数目
		long fruit = playerLand.getFruitMax() - (playerLand.getFruitStealMax() - playerLand.getFruitStealLeft());
		// 保存仓库
		if (EmptyHelper.isEmpty(playerWarehouseProp)) {
			playerWarehouseProp = new Sfg661030PlayerWarehouseDBO();
			playerWarehouseProp.prepareNumeric();
			playerWarehouseProp.setPlayerId(player.getUserId());
			playerWarehouseProp.setPlayerName(player.getUserName());
			// 物品ID
			playerWarehouseProp.setPropId(playerLand.getCropsId());
			// 物品名称
			playerWarehouseProp.setPropName(playerLand.getCropsName());
			// 设置物品类别(1农作物2家畜3水产5产品9其他)
			playerWarehouseProp.setPropType(ONE);
			// 设置单一物品占用仓库容积大小
			playerWarehouseProp.setPropVolume(playerLand.getFruitVolume());
		}

		// 设置果实数目
		playerWarehouseProp.setPropNum(playerWarehouseProp.getPropNum() + fruit);

		// 动作增加经验, 农场升级信息
		SFGGameThrusterServiceHelper.preparePlayerFarmTodayExp(sfgGameInfoCacheService, sfgPlayerInfoCacheService, playerFarm, RandomUtils.nextInt(0, playerLand.getCropsExp().intValue()));

		// 修正土地状态
		Long landSort = playerLand.getLandSort();
		{
			playerLand = new Sfg661021PlayerLandDBO();
			playerLand.setPlayerId(player.getUserId());
			playerLand.setPlayerName(player.getUserName());
			playerLand.setLandState(NINE);// 1未开垦2未种植3成长中5成熟待收割9收割后未翻地
			playerLand.setLandSort(landSort);
		}

		// 设置返回信息
		result.setMsg(ACTION_OK_MESSAGE);
		// 用户数据集中保存
		sfgPlayerInfoCacheService.savePlayer(player, playerFarm, playerLand, playerWarehouseProp, null);
		return result;
	}

	/**
	 * 添加好友
	 */
	public RESTResultBean<BoxySFGPlayer> doPlayerFriend(BoxySFGPlayer toPlayer) throws Exception {
		RESTResultBean<BoxySFGPlayer> result = new RESTResultBean<BoxySFGPlayer>();

		// 简单处理，便于维护
		BoxySFGPlayer player = new BoxySFGPlayer();
		player.setId(toPlayer.getPlayerFriendId());
		Sfg661010PlayerFarmDBO playerFarm = sfgPlayerInfoCacheService.loadPlayerFarm(player);

		// 添加好友
		if (EmptyHelper.isNotEmpty(playerFarm)) {
			SfgPlayerFriendDBO friend = new SfgPlayerFriendDBO();
			friend.setPlayerFriendId(playerFarm.getPlayerId());
			friend.setPlayerFriendName(playerFarm.getPlayerName());
			sfgPlayerInfoCacheService.savePlayerFriend(toPlayer, friend);
			result.setMsg(ACTION_MESSAGE_Friend);
		} else {
			result.setCode(1);
			result.setMsg(ACTION_FAIL_MESSAGE);
		}
		return result;
	}

}
